package com.dlmz.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import com.dlmz.util.BaseDao;
import com.dlmz.util.ExcelExporter;

public class HealthformManage extends JFrame implements ActionListener{

	private JLabel queryLab;  
    private JTextField queryTxt;  
    private JTable resultTb;  
    private JScrollPane jsp;  
    private JButton addBtn,deleteBtn,updateBtn,allBtn,queryBtn,count;  
    private JPanel top,bottom;   
    //========  
    private HealModel hm;  
    String sql = "SELECT Hno,b.`Sno`,a.`Sname`,Height,Weight,Chest,BPressure,Tracm,CX,Pdate,Presult,Medicalhistory "
    		+ "FROM student AS a,healthform AS b WHERE a.`Sno`=b.`Sno` order by Hno"; 
    //构造函数  
    public HealthformManage()  
    {  
        super("体检管理系统");
        /***************************初始化面板控件***********************/  
        //========查询栏  
        queryLab = new JLabel("请输入体检号或姓名或学号:");  
        queryTxt = new JTextField(10);  
        queryBtn = new JButton("查询");  
        allBtn = new JButton("全部显示");  
        //......添加查询栏监听  
        queryBtn.addActionListener(this);  
        queryBtn.setActionCommand("query");  //确定使用的组件
        allBtn.addActionListener(this);  
        allBtn.setActionCommand("all");  
        //========增删改统计栏  
        addBtn = new JButton("添加");  
        deleteBtn = new JButton("删除");  
        updateBtn = new JButton("修改"); 
        count = new JButton("打印");
        //......添加增删改统计栏监听  
        addBtn.addActionListener(this);  
        addBtn.setActionCommand("add");  
        deleteBtn.addActionListener(this);  
        deleteBtn.setActionCommand("delete");  
        updateBtn.addActionListener(this);  
        updateBtn.setActionCommand("update");  
        count.addActionListener(this);
        count.setActionCommand("count"); 
  
        //========查询栏  
        top = new JPanel();  
        top.add(queryLab);  
        top.add(queryTxt);  
        top.add(queryBtn);  
        top.add(allBtn);  
        //......底层增删改统计栏  
        bottom = new JPanel();  
        bottom.add(addBtn);  
        bottom.add(deleteBtn);  
        bottom.add(updateBtn); 
        bottom.add(count);
        //......中间层显示栏  
        hm = new HealModel();  
        hm.queryHeal(sql);  
        resultTb = new JTable(hm);  
        jsp = new JScrollPane(resultTb);  
        //......构建整体布局  
        this.add(top,BorderLayout.NORTH);  
        this.add(jsp,BorderLayout.CENTER);  
        this.add(bottom,BorderLayout.SOUTH);  
        //========设置窗口属性  
        validate();
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setSize(1000,400);
		this.setLocationRelativeTo(null);
		setVisible(true);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				if (JOptionPane.showConfirmDialog(HealthformManage.this, "是否退出体检系统？")==JOptionPane.OK_OPTION) {
					dispose();
					new MenuJframe();
				}else {
					new HealthformManage();
				}
			}
		});
    }  
      
    //监听  
    public void actionPerformed(ActionEvent e)  
    {  
        // TODO Auto-generated method stub  
        if(e.getActionCommand().equals("query")) {  
            /*********************查询***********************/  
            //========获取输入学生的姓名  
            String name = queryTxt.getText().trim();  //trim()去除字符串的头尾空格
            String id = queryTxt.getText().trim();
            String hno = queryTxt.getText().trim();
            if(name.length() != 0 ||id.length() != 0||hno.length() != 0) {  
                //========姓名输入有效时，执行查询  
                //......定义参数  
                String sql2 = "select * from ("+sql+") As c where Hno=? or Sno=? or Sname =?"; 
                BaseDao baseDao=new BaseDao();
                //......更新模型  
                jtableUpdate(sql2,hno,id,name);  
            } else {  
                //========姓名为空时，设置提醒  
                JOptionPane.showMessageDialog(this, "姓名输入不能为空");  
            }  
        } else if(e.getActionCommand().equals("add")) {  
            /*********************添加***********************/  
            new HealAddDialog(this, "添加体检信息", true);  
            jtableUpdate(sql, null);  
        } else if(e.getActionCommand().equals("all")) {  
            /*********************全部显示***********************/  
            jtableUpdate(sql, null);  
        }else if(e.getActionCommand().equals("count")) {
        	/*********************打印体检信息***********************/ 
			try {
			ExcelExporter exp = new ExcelExporter();
			exp.exportTable(resultTb, new File("体检信息表.txt"));
			}catch (IOException ex) {
				 System.out.println(ex.getMessage());
				 ex.printStackTrace();
			}
        }
        else if(e.getActionCommand().equals("delete")) {  
            /*********************删除***********************/  
            //========获取选择行号  
            int rowNum = this.resultTb.getSelectedRow();  
            if(rowNum == -1) {  
                JOptionPane.showMessageDialog(this, "请选择一行");  
                return ;  
            }  
            //========获取学生学号  
            String hid = (String)hm.getValueAt(rowNum, 0); 
            BaseDao basedao=new BaseDao();
            //========删除学生  
            String sql3 = "delete from healthform where Hno=?";  
            int num=basedao.update(sql3, hid);
			if(num>0) {
				JOptionPane.showMessageDialog(null, "删除体检信息成功！");
			}else {
				JOptionPane.showMessageDialog(null, "删除体检信息失败！");
			} 
            HealModel tmp = new HealModel();  
            //tmp.cudStu(sql, paras);  
            //========更新模型    
            jtableUpdate(sql, null);  
        } else if(e.getActionCommand().equals("update")) {  
            /*********************修改***********************/  
            //========获取选择行号  
            int rowNum = this.resultTb.getSelectedRow();  
            if(rowNum == -1) {  
                JOptionPane.showMessageDialog(this, "请选择一行");  
                return ;  
            }  
            	new HealUpdateDialog(this, "修改体检信息", true, hm, rowNum);   
            jtableUpdate(sql, null); 
        }   
    }  
      
    //========更新JTable内数据  
    public void jtableUpdate(String sql,Object...objects)  
    {  
        //......创建模型  
        hm = new HealModel();  
        hm.queryHeal(sql,objects);  
        //......更新显示  
        resultTb.setModel(hm);  
    }  
    public static void main(String args[]) {
    	new HealthformManage();
    }
}
